var result;
var isVip = 0;
isVip === 0 ? $('.aui-flex').css({
    display: 'none'
}) : console.log(isVip); //是否显示返利广告
function revlink(obj) { //解析单品请求结果 不含海报
    // eslint-disable-next-line no-undef
    $.get('https://quan618.cn/PHPSDK/revlink.php', {
        'goodsId': function () {
            return typeof (obj) == 'object' ? obj.goodsId : obj;
        }
    }, function (f) {
        f = JSON.parse(f);

        if (f.msg === '成功') {
            result = f.data;
            $.extend(true, result, obj);
            //console.log(result);
            var taowords = '仅需' + result.actualPrice + '元 ' + result.tpwd;
            var url = decodeURIComponent(result.shortUrl);
            //$('#coupons').attr('href', url);
            let image = (function () {
                if (result.mainPic) {
                    return decodeURIComponent(result.mainPic);
                } else if (result.marketingMainPic) {
                    return decodeURIComponent(result.marketingMainPic);
                } else if (result.marketingMainPic == undefined || result.marketingMainPic == '') {
                    return 'https://008ts.cn/ksym/loading.jpg';
                }
            })();
            $('#img').attr('src', image);
            $('.title>span').text('仅需' + result.actualPrice + '元 ' +
                result.title);
            $('#copy').on('click', function () {
                $(this).attr('data-clipboard-text', result.tpwd);
                setTimeout(function () {
                    try {
                        location.href = url;
                    } catch (e) {
                        console.log(e);
                    }

                }, 500);
            });
            $('#taokey').text(taowords);
        } else {
            alert('出现异常，请F12查看console');
            console.log(f.msg);
        }
    });

}

//请求排行榜数据
function parseRank(e) {
    //获取top1单品的首图，标题
    e = JSON.parse(e);
    if (e.msg == '成功') {
        revlink(e.data[0]);
    } else {
        alert('出现异常，请F12查看console');
        console.log(e.msg);
    }
}

function parseQueryString(url, paramsKey) {
    // 先将字符串通过 split 方法，以 "?" 为分割符将其分割成数组；
    // arr有两个元素，第一个为url源地址，第二个为后边的参数字符串
    if (url == undefined || url == '') url = location.href;
    if (paramsKey == undefined || url == '') paramsKey == goodsId;
    if (location.search == '') return null;
    let arr = url.split('?');
    // 将参数字符串以 "&" 符号为分隔符再次进行进行分割
    let params = arr[1].split('&');
    // 定义一个数组用于存储参数
    let obj = {};
    // 通过循环将参数以键值对的形式存储在变量 obj 中
    for (let i = 0; i < params.length; i++) {
        let arr_params = params[i].split('=');
        obj[arr_params[0]] = arr_params[1];
    }
    return obj[paramsKey] !== undefined ? obj[paramsKey] : obj;
}

var goodsId = parseQueryString(location.href, 'goodsId');
goodsId == null ? $.get('https://quan618.cn/PHPSDK/autoapi.php', {
    apitype: 'rank',
    num: 1,
    p: 1
}, parseRank) : revlink(goodsId);

//将revlink 放到sku中执行  revlink 中接收的始终是对象
window.site = {
    'id': 'cs',
    'url': window.location.host,
    'ip': returnCitySN["cip"],
    'page': 'PMD'
}
// eslint-disable-next-line no-undef

$(function () {
    var isShow = 0;
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/iphone/i) == "iphone" || ua.match(/ipad/i) == "ipad") {
        var iphoneInfo = ua.match(/iphone os (\d{1,})/i);
        var iosVersion = iphoneInfo[1];
        if (iosVersion >= 10 || ua.match(/ipad/i) == 'ipad') {
            $('.itemCopy').show();
        }
    } else {
        $('.itemCopy').show();
    }
});
